//
//  YDStatusCell.swift
//  YDWeibo01
//
//  Created by suibian on 16/8/13.
//  Copyright © 2016年 iitcast. All rights reserved.
//

import UIKit
import SnapKit
class YDStatusCell: UITableViewCell {
    
//    记录约束 底部视图的顶部约束
    var bottomViewTopConstraint:Constraint?
    var statusViewModel:YDStatusViewModel?{
        didSet{
//        原创微博
        originalView.statusViewModel = statusViewModel
//        转发微博
        retweetView.statusViewModel = statusViewModel
//            转发微博逻辑分析
//            卸载约束
            bottomViewTopConstraint?.uninstall()
//            如果没有转发微博
            if statusViewModel?.statusModel?.retweeted_status == nil{
            bottomView.snp_updateConstraints(closure: { (make) -> Void in
                bottomViewTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
            })
//                隐藏转发微博
                retweetView.hidden = true
//            如果有转发微博
            }else{
//             先赋值
                retweetView.statusViewModel = statusViewModel
            bottomView.snp_updateConstraints(closure: { (make) -> Void in
                bottomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            })
//            显示转发微博
                retweetView.hidden = false
            }
//            底部视图赋值
            bottomView.statusViewModel = statusViewModel
        }
    
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    设置视图
    private func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
//        添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
//        添加约束
    originalView.snp_makeConstraints { (make) -> Void in
        make.leading.top.trailing.equalTo(contentView)
//        make.height.equalTo(80) //已经在自己的类中设置
    
        }
//        02
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
//            make.height.equalTo(50)
        }
//    03
        bottomView.snp_makeConstraints { (make) -> Void in
//            记录约束
            bottomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
    contentView.snp_makeConstraints { (make) -> Void in
        make.leading.top.trailing.equalTo(self)
        make.bottom.equalTo(bottomView)
        }
    
    }
    
//    懒加载控件
//    原创微博
    private lazy var originalView:YDStadusOriginalView = YDStadusOriginalView()
//    转发微博
    private lazy var retweetView:YDStatusRetweetView = YDStatusRetweetView()
//    底部视图
    private lazy var bottomView:YDStatusBottomView = YDStatusBottomView()

}
